package com.wn.icloud.auth.configuration;

import com.wn.icloud.auth.interceptor.ServiceAuthRestInterceptor;
import com.wn.icloud.auth.interceptor.UserAuthRestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author leeyang
 * @date 2017/9/8
 */
@Configuration("admimWebConfig")
@Primary
public class WebConfiguration implements WebMvcConfigurer {


    @Value("${auth.disable-server-auth:true}")
    private Boolean disableServerAuth;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!disableServerAuth) {
            registry.addInterceptor(getServiceAuthRestInterceptor()).addPathPatterns("/service/**");
        }
        registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns("/service/**");
    }

    @Bean
    ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
        return new ServiceAuthRestInterceptor();
    }

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }

}
